﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.Framework.Utilities.Report
{
    public class TableTR : ElementContainer
    {
        #region "字段"

        /// <summary>
        /// {0}:tr内部的内容
        /// </summary>
        private String tr = "<tr>{0}</tr>";

        #endregion

        #region "公共方法"

        /// <summary>
        /// 把元素转换成HTML
        /// </summary>
        public String ToHtml(DataTable data)
        {
            if (this.ElementType == Enums.ElementType.TableTitleTR)
                return this.CreateTitleTR();
            else if (data != null && this.ElementType == Enums.ElementType.TableDataTR)
                return this.CreateDataTR(data);
            return String.Empty;
        }

        #endregion

        #region "私有方法"

        /// <summary>
        /// 根据数据创建数据列的内容
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private String CreateDataTR(DataTable data)
        {
            Int32 columnCount = data.Columns.Count;
            StringBuilder trSB = new StringBuilder();
            StringBuilder tdSB = null;
            List<DataColumn> columnList = new List<DataColumn>();
            List<TableTD> tdList = new List<TableTD>();
            for (Int32 j = 0; j < elements.Count; j++)
            {
                DataColumn col = null;
                if (elements[j].ElementType == Enums.ElementType.TableDataTD || elements[j].ElementType == Enums.ElementType.TableTitleTD)
                {
                    TableTD td = ElementHelper.Create<TableTD>(elements[j]);
                    if (MyDataTable.TryGetColumnByName(data, td.FieldName, out col, StringComparison.CurrentCultureIgnoreCase))
                    {
                        columnList.Add(col);
                        tdList.Add(td);
                    }
                }
            }
            Int32 rowCount = data.Rows.Count;
            for (Int32 i = 0; i < rowCount; i++)
            {
                tdSB = new StringBuilder();
                for (Int32 j = 0, k = columnList.Count; j < k; j++)
                {
                    TableTD tempTD = tdList[j];
                    tdSB.Append(tempTD.ToHtml(data.Rows[i][tempTD.FieldName]));
                }
                trSB.AppendFormat(tr, tdSB.ToString());
            }
            return trSB.ToString();
        }

        /// <summary>
        /// 根据数据创建标题列的内容
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private String CreateTitleTR()
        {
            StringBuilder sb = new StringBuilder();
            for (Int32 j = 0; j < elements.Count; j++)
            {
                if (elements[j].ElementType == Enums.ElementType.TableDataTD || elements[j].ElementType == Enums.ElementType.TableTitleTD)
                {
                    TableTD td = ElementHelper.Create<TableTD>(elements[j]);
                    sb.Append(td.ToHtml(td.FieldName));
                }
            }
            return String.Format(tr, sb.ToString());
        }

        #endregion
    }
}
